﻿// <copyright file="BodyType.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-24</date>
// <summary>OpenSocial BodyType class</summary>

namespace opensocial
{
    using System.Globalization;
    using System.Windows.Browser;
    using Synergist;

    /// <summary>
    /// The OpenSocial BodyType
    /// </summary>
    public class BodyType
    {
        /// <summary>
        /// Prefix for opensocial.BodyType fields
        /// </summary>
        private const string Prefix = "opensocial.BodyType.Field.";

        /// <summary>
        /// the base JavaScript openSocial.BodyType object
        /// </summary>
        private ScriptObject scriptObject;

        /// <summary>
        /// Initializes a new instance of the BodyType class.
        /// </summary>
        public BodyType()
        {
        }

        /// <summary>
        /// Initializes a new instance of the BodyType class.
        /// </summary>
        /// <param name="baseObject">the base JavaScript openSocial.BodyType object</param>
        public BodyType(ScriptObject baseObject)
        {
            if (baseObject == null)
            {
                return;
            }

            this.scriptObject = baseObject;
            this.Build = this.getField("BUILD") as string;
            this.EyeColor = this.getField("EYE_COLOR") as string;
            this.HairColor = this.getField("HAIR_COLOR") as string;
            object height = this.getField("HEIGHT");

            if (height != null)
            {
                this.Height = (double)height;
            }

            object weight = this.getField("WEIGHT");

            if (weight != null)
            {
                this.Weight = (double)weight;
            }
        }

        /// <summary>
        /// Gets or sets the build of the person
        /// </summary>
        public string Build { get; set; }

        /// <summary>
        /// Gets or sets the eye color of the person
        /// </summary>
        public string EyeColor { get; set; }

        /// <summary>
        /// Gets or sets the hair color of the person
        /// </summary>
        public string HairColor { get; set; }

        /// <summary>
        /// Gets or sets the height of the person in meters, specified as a number.
        /// </summary>
        public double? Height { get; set; }

        /// <summary>
        /// Gets or sets the weight of the person in kilograms, specified as a number.
        /// </summary>
        public double? Weight { get; set; }

        /// <summary>
        /// Gets a field value
        /// </summary>
        /// <param name="key">the opensocial.BodyType.Field</param>
        /// <returns>the value in the field</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public object getField(string key)
        {
            string keyValue = string.Format(CultureInfo.InvariantCulture, "{0}{1}", Prefix, key).Eval();

            return this.scriptObject.Invoke("getField", keyValue);
        }

        /// <summary>
        /// Gets the properties for the object
        /// </summary>
        /// <returns>a string listing the non-null properties and their values.</returns>
        public override string ToString()
        {
            return Synergist.Utility.GetProperties(this);
        }
    }
}
